package cn.jy.nvr.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.APHERESES;
import cn.jy.nvr.domain.APHERESESTask;
import cn.jy.nvr.domain.JZMACHINE;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.UserSet;
import cn.jy.nvr.mapper.APHERESESMapper;
import cn.jy.nvr.mapper.APHERESESTaskMapper;
import cn.jy.nvr.mapper.JZMACHINEMapper;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.UserSetMapper;
import cn.jy.nvr.service.ScanCollectionRecordQuartzJob;
import cn.jy.nvr.utils.DateTools;
import cn.jy.nvr.utils.TimeUtils;
/**
 * @author zyl
 *  time:2018年5月25日14:31:02
 *  定时扫描采浆任务，生成视频下载任务
 */
@Service("ScanCollectionRecordQuartzJob")
public class ScanCollectionRecordQuartzJobImpl implements
		ScanCollectionRecordQuartzJob {
	@Autowired
      JZMACHINEMapper jzmachineMapper;
	@Autowired
      APHERESESMapper apheresesMapper;
	@Autowired
	ProviderBaseInfoMapper baseInfoMapper;
	@Autowired
	APHERESESTaskMapper apheresesTaskMapper;
	@Autowired
	UserSetMapper userSetMapper;
 	Logger logger=Logger.getLogger(ScanCollectionRecordQuartzJobImpl.class);
	@Override
	public void work() {
		
		
		
		logger.info("---开始进行扫描任务---");
		DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<JZMACHINE> loadJZMACHINEOnUse = jzmachineMapper.loadJZMACHINEOnUse();
		logger.info("---加载设备信息完成---");
		for (JZMACHINE jzmachine : loadJZMACHINEOnUse) {
			
			APHERESES aPHERESES=new APHERESES();
			aPHERESES.setMAC_ID(jzmachine.getID());
			aPHERESES.setSTART_TIME( DateTools.getInstance().dateFormat(new Date(), "yyyy-MM-dd"));
			DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
			List<APHERESES> loadAPHERESESByMachineAndDate = apheresesMapper.loadAPHERESESByMachineAndDate(aPHERESES);
			logger.info("获取到"+jzmachine.getNAME()+"所有采浆"+loadAPHERESESByMachineAndDate.size());
			if (loadAPHERESESByMachineAndDate.size()>0) {
				
				for (int i = 0; i < loadAPHERESESByMachineAndDate.size(); i++) {
					
					APHERESES aphereses = loadAPHERESESByMachineAndDate.get(i);
					String  collectionStartTime=aphereses.getSTART_TIME();
					DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
					ProviderBaseInfo providerBaseInfoByID = baseInfoMapper.getProviderBaseInfoByID(aphereses.getDONOR_ID());
					if (providerBaseInfoByID==null) {
						continue;
					}
					String cardId=providerBaseInfoByID.getCARDID();
					APHERESESTask apheresesTask=new APHERESESTask();
					apheresesTask.setCardId(cardId);
					apheresesTask.setCollectionStartTime(collectionStartTime.substring(0, 10));
					DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
					APHERESESTask loadAPHERESESTask = apheresesTaskMapper.loadAPHERESESTask(apheresesTask);
					 if (loadAPHERESESTask==null) {
						//i等于0说明是今天这台机器第一采集是没得空闲时间的
						 apheresesTask.setProviderName(providerBaseInfoByID.getXM());
						 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
						 UserSet loadNurse = userSetMapper.loadNurse(aphereses.getUSERID());
						 if (loadNurse!=null) {
							 apheresesTask.setNurse(loadNurse.getPERSONNAME());
						}
						 //机器号
						 apheresesTask.setMachineNo(jzmachine.getNAME());
						 //安装耗材时间
						  apheresesTask.setInstallStartTime(aphereses.getINSTALLSTARTTIME());
						  apheresesTask.setInstallEndTime(aphereses.getINSTALLENDTIME());
						  apheresesTask.setInstallTakens(aphereses.getINSTALLTAKEN());
						  //安装耗材后的空闲时间
						  apheresesTask.setInstallSpaceStartTime(aphereses.getINSTALLENDTIME());
						  apheresesTask.setInstallSpaceEndTime(aphereses.getPUNCTURESTARTTIME());
						   String spaceinstallSeconds = TimeUtils.getInputDateOutSeconds(aphereses.getPUNCTURESTARTTIME(), aphereses.getINSTALLENDTIME());
						  apheresesTask.setInstallSpaceTakens(spaceinstallSeconds);
						  
						  //穿刺时间
						 apheresesTask.setPunctureStartTime(aphereses.getPUNCTURESTARTTIME());
						 apheresesTask.setPunctureEndTime(aphereses.getPUNCTUREENDTIME());
						 apheresesTask.setPunctureTakens(aphereses.getPUNCTURETAKEN());
						 
						 //采浆时间
						 apheresesTask.setCollectionStartTime(aphereses.getSTART_TIME());
						 apheresesTask.setCollectionEndTime(aphereses.getEND_TIME());
						 apheresesTask.setCollectionCycles(aphereses.getCYCLES());
						  apheresesTask.setCollectionTakens(aphereses.getCOLLTAKEN());
						 
						  //卸载耗材时间
						  apheresesTask.setUninstallStartTime(aphereses.getUNINSTALLSTARTTIME());
						  apheresesTask.setUninstallEndTime(aphereses.getUNINSTALLENDTIME());
						  apheresesTask.setUninstallTakens(aphereses.getUNINSTALLTAKEN());
						  
						  //i>0就有空闲时间
						if (i>0) {
							APHERESES apheresesBefore = loadAPHERESESByMachineAndDate.get(i-1);
							String spaceStartTime = apheresesBefore.getUNINSTALLENDTIME();
							String spaceEndTime = aphereses.getINSTALLSTARTTIME();
							apheresesTask.setSpaceStartTime(spaceStartTime);
							apheresesTask.setSpaceEndTime(spaceEndTime);
							String spaceSeconds = TimeUtils.getInputDateOutSeconds(spaceEndTime, spaceStartTime);
							apheresesTask.setSpaceTakens(spaceSeconds);
							apheresesTask.setSpaceSequence(String.valueOf(i));
						} 
						
						logger.info("保存"+apheresesTask.getProviderName()+"采浆信息");
						DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
						apheresesTaskMapper.save(apheresesTask);
					}
					
					
				}
				
				
			}
			
			
		}
		
		
		
		
		
	}

}
